#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define x first 
#define y second
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int h[N], ne[N], w[N], e[N], index;
int dis[N];
bool st[N];
int n, m;

void add(int a, int b, int u)
{
	e[index] = b;
	ne[index] = h[a];
	w[index] = u;
	h[a] = index++;
}
int SPFA()
{
	memset(dis, 0x3f, sizeof(dis));
	dis[1] = 0;
	queue<int>p;
	p.push(1);
	st[1] = 1;
	while (p.size()) {
		int t = p.front();
		p.pop();
		st[t] = 0;
		for (int i = h[t]; i != -1; i = ne[i]) { 
			int j = e[i];
			if (dis[j] > dis[t] + w[i]) {
				dis[j] = dis[t] + w[i];
				if (!st[j]) {
					p.push(j);
					st[j] = 1;
				}
			}
		}
	}
	if (dis[n] == 0x3f3f3f3f)return -1;
	return dis[n];
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	while (m--) {
		int a, b, u;
		cin >> a >> b >> u;
		add(a, b, u);
	}
	int ret = SPFA();
	if (ret == -1) {
		cout << "impossible" << endl;
	}
	else {
		cout << ret << endl;
	}
	return 0;
}